#!/bin/bash

set -e

base_dir="/home/caibh/gitee/ems-archive" 

# 引入封装的逻辑
source $base_dir/bin/common.sh

# 初始化
cm_init

# 加载配置文件
cm_load_properties "archive-iot-ts.properties"

#
# 查询超级表
#
# 参数:
#     $1: 物联网系统数据库的具体名称，默认 iot_ts
#     $2: 超级表分类。core，other。core 代表 properties_<e|w|g>_modbus 这三个表，other 则是除这三个表之外的其它表
#
function query_stbs(){
    local db="$1"
    local category="$2"
    local sql=""

    case "$category" in
    "core")
        sql="select distinct stable_name from information_schema.ins_stables where db_name = '$db' and stable_name in ('properties_e_modbus','properties_w_modbus','properties_g_modbus') order by stable_name;" ;;
    "other")
        sql="select distinct stable_name from information_schema.ins_stables where db_name = '$db' and stable_name not in ('properties_e_modbus','properties_w_modbus','properties_g_modbus') order by stable_name;" ;;
    *)
        echo "ERR: 无法识别超级表分类参数 category [$category]" >&2 ;;
    esac

    cm_exec_sql_tdengine "$db" "$sql" | cm_del_conn_info | cm_del_head_info "stable_name"
}

query_stbs iot_ts other